<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:c="livcos.org/ns/cosmos"
  xmlns:m="livcos.org/ns/map"
  xmlns:f="livcos.org/ns/xslt/function"
  exclude-result-prefixes="f m c">
  
  <xsl:import href="../../../common/base.xsl"/>
	
	<!-- JSON Array with all map item labels -->
  
  <xsl:output method="text"/>

  <xsl:param name="ref"/>
  
  <xsl:template match="/">
    <xsl:choose>
    	<xsl:when test="f:object-available($ref)">
        <xsl:text>[</xsl:text>
        <xsl:apply-templates select="doc($ref)/*//m:item[m:label]"/>
        <xsl:text>]</xsl:text>
      </xsl:when>
      <xsl:otherwise>
        <xsl:text>{"error": "ObjectNotFound", "message": "Object </xsl:text>
        <xsl:value-of select="$ref"/>
        <xsl:text> not found!"}</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template match="m:item">
    <xsl:if test="position() > 1">,</xsl:if>
    <xsl:text>"</xsl:text>
    <xsl:value-of select="normalize-space(replace(replace(m:label, '''', '\\'''), '\n', ' '))"/>
    <xsl:text>"</xsl:text>
  </xsl:template>
  
</xsl:stylesheet>
